/* eslint-disable @typescript-eslint/no-unused-vars */
import { useRef } from 'react';
import { Modal, ModalProps } from 'antd';

export type ModalType = 'info' | 'success' | 'error' | 'warning' | 'confirm';
interface innerConfig extends Omit<ModalProps, 'onOk'> {
  onOk?: () => Promise<any>;
}
type ItriggerModal = (mode: ModalType, config: innerConfig) => void;

export default function useModal() {
  const locked = useRef(false);
  const triggerModal: ItriggerModal = (mode, config) => {
    const { onOk, ...restProps } = config;
    locked.current = false;
    Modal[mode]({
      ...restProps,
      onOk() {
        if (locked.current) return Promise.resolve();
        locked.current = true;
        return onOk && onOk();
      },
    });
  };

  return triggerModal;
}
